package com.zzt.lotterysystem.controller;

import com.zzt.lotterysystem.common.pojo.CommonResult;
import com.zzt.lotterysystem.common.utils.JacksonUtil;
import com.zzt.lotterysystem.controller.param.DrawPrizeParam;
import com.zzt.lotterysystem.controller.param.ShowWinningRecordsParam;
import com.zzt.lotterysystem.controller.result.WinningRecordsResult;
import com.zzt.lotterysystem.service.DrawPrizeService;
import com.zzt.lotterysystem.service.dto.WiningRecordDTO;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.CollectionUtils;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;

/**
 * Created with IntelliJ IDEA.
 * Description
 * User: panda
 * Date: 2025-05-28
 * Time: 22:36
 */
@RestController
public class DrawPrizeController {

    private final static Logger logger = LoggerFactory.getLogger(DrawPrizeController.class);

    @Autowired
    private DrawPrizeService drawPrizeService;

    @RequestMapping("/draw-prize")
    public CommonResult<Boolean> drawPrize(
            @Validated @RequestBody DrawPrizeParam param) {
        logger.info("drawPrize param:{}", param);
        // service
        drawPrizeService.drawPrize(param);
        return CommonResult.success(true);
    }

    @RequestMapping("/winning-records/show")
    public CommonResult<List<WinningRecordsResult>> showWinningRecords(
            @Validated @RequestBody ShowWinningRecordsParam param) {
        logger.info("showWinningRecords param:{}", JacksonUtil.writeValueAsString(param));
        List<WiningRecordDTO> winingRecordDTOList = drawPrizeService.getRecords(param);
        return CommonResult.success(convertToWinningRecordsResult(winingRecordDTOList));
    }

    private List<WinningRecordsResult> convertToWinningRecordsResult(List<WiningRecordDTO> winingRecordDTOList) {
        if (CollectionUtils.isEmpty(winingRecordDTOList)) {
            return Arrays.asList();
        }
        return winingRecordDTOList.stream()
                .map(
                        winingRecordDTO -> {
                            WinningRecordsResult result = new WinningRecordsResult();
                            result.setWinnerId(winingRecordDTO.getWinnerId());
                            result.setWinnerName(winingRecordDTO.getWinnerName());
                            result.setPrizeName(winingRecordDTO.getPrizeName());
                            result.setPrizeTier(winingRecordDTO.getPrizeTier().getMessage());
                            result.setWinningTime(winingRecordDTO.getWinningTime());
                            return result;
                        }
                ).collect(Collectors.toList());
    }
}
